package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 套餐分页查询功能
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询接口")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐功能
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐接口")
    public Result add(@RequestBody SetmealDTO setmealDTO){
        setmealService.add(setmealDTO);
        return Result.success();
    }

    /**
     * 删除套餐功能
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐接口")
    public Result delByIds(@RequestParam List<Long> ids){
        setmealService.delByIds(ids);
        //删除套餐之后要删除redis对应的数据
        ids.forEach(id->{
            //根据id获取每一个categoryId做对应的缓存删除
            SetmealVO setmealVO = setmealService.findById(id);
            String CACHE_SETMEAL_KEY = "SETMEAL_" + setmealVO.getCategoryId();
            redisTemplate.delete(CACHE_SETMEAL_KEY);
        });
        return Result.success();
    }

    /**
     * 修改套餐功能
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐接口")
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        //修改套餐之后删除redis对应的数据
        String CACHE_SETMEAL_KEY = "SETMEAL_" + setmealDTO.getCategoryId();
        redisTemplate.delete(CACHE_SETMEAL_KEY);
        return Result.success();
    }

    /**
     * 根据id查询套餐功能
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐接口")
    public Result<SetmealVO> findById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.findById(id);
        return Result.success(setmealVO);
    }

    /**
     * 启售或停售套餐功能
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启售或停售套餐接口")
    public Result enableOrDisable(@PathVariable Integer status,Long id){
        setmealService.enableOrDisable(status,id);
        //启售或停售之后要删除redis对应的数据
        SetmealVO setmealVO = setmealService.findById(id);
        String CACHE_SETMEAL_KEY = "SETMEAL_" + setmealVO.getCategoryId();
        redisTemplate.delete(CACHE_SETMEAL_KEY);
        return Result.success();
    }
}
